﻿Imports Magic.Core
Imports SlimDX.Direct3D9
Imports SlimDX


Public Class Obje
    Implements IRenderable
    Dim objModel As Model
    Public name As String
    Private UnseenModel As Boolean = False
    Sub New(ByVal objTag As Tag)
        AddHandler objTag.TagHasChanged, AddressOf Changed
        Dim UID As UInteger = magicMap.Library.IDByTag(objTag)
        Dim tagID As Integer = magicMap.Library.IndexByID(UID)
        name = magicMap.Library.GetName(tagID)
        Dim modeIndex = objTag("model")

        If name.Contains("scenery\emitters\") Then
            UnseenModel = True
        Else
            objModel = Managers.ReturnModel(modeIndex)
        End If
    End Sub
    Sub Changed(ByVal objTag As Magic.Core.Tag)
        Dim UID As UInteger = magicMap.Library.IDByTag(objTag)
        Dim tagID As Integer = magicMap.Library.IndexByID(UID)
        name = magicMap.Library.GetName(tagID)
        Dim modeIndex = objTag("model")

        If name.Contains("scenery\emitters\") Then
            UnseenModel = True
        Else
            objModel = Managers.ReturnModel(modeIndex)
        End If
    End Sub
    Sub Render(ByVal xDevice As Device) Implements IRenderable.Render
        If Not objModel Is Nothing AndAlso Not UnseenModel Then
            objModel.Render(xDevice)
        ElseIf UnseenModel Then
            Dim pMatrix As Matrix = xDevice.GetTransform(TransformState.World)
            pMatrix = Matrix.Multiply(Matrix.Scaling(0.01F, 0.01F, 0.01F), pMatrix)
            xDevice.SetTransform(TransformState.World, pMatrix)
            Scenario.RenderBox(Color.SteelBlue)
        End If
    End Sub
    Sub OccludeRender(ByVal xDevice As Device)
        If Not objModel Is Nothing AndAlso Not UnseenModel Then
            objModel.OccludeRender(xDevice)
        ElseIf UnseenModel Then
            Dim pMatrix As Matrix = xDevice.GetTransform(TransformState.World)
            pMatrix = Matrix.Multiply(Matrix.Scaling(0.01F, 0.01F, 0.01F), pMatrix)
            xDevice.SetTransform(TransformState.World, pMatrix)
            boxMesh.DrawSubset(0)
        End If
    End Sub
    Sub Dispose()
        objModel.Dispose()
    End Sub
    Function Intercept(ByVal eX As Integer, ByVal eY As Integer, ByVal pMatrix As Matrix, ByRef Distance As Single) As Boolean
        Distance = Single.MaxValue
        If IsNothing(objModel) Then
            If UnseenModel Then
                pMatrix = Matrix.Multiply(Matrix.Scaling(0.01F, 0.01F, 0.01F), pMatrix)
                Dim nRay As Ray = CreateRay(pMatrix, eX, eY)
                If boxMesh.Intersects(nRay, Distance) Then
                    Return True
                End If
            Else
                Return False
            End If
        Else
            Distance = objModel.Intercept(CreateRay(pMatrix, eX, eY))
            If Distance = Single.MaxValue Then Return False
            Return True
        End If
    End Function
End Class
